<?php
namespace Admin\Controller;


use Common\Model\AdminModel;
use Think\Controller;

/**
 * 检测后台登陆状态控制器
 */
class CommonController extends Controller {

	public function __construct(){
		parent::__construct();
		$IsLogin = $this->IsLogin(true,'aid');
		$this->loginUserInfo = $IsLogin;
	}

	public function IsLogin($jump=true,$id=null){
		$id = $id == null ? 'uid' : $id;
		$key = sprintf('%s_%s',C('COOKIEVALIDATIONKEY'),$id);
		$userCookie = cookie($key) ? cookie($key) : '';

		if($userCookie){
			$sValue = isset($_SESSION[$userCookie]) && !empty($_SESSION[$userCookie]) ? $_SESSION[$userCookie] :"";
			if($sValue){
				$userCookieTemp = explode('_',$userCookie);
				$sValueTemp = explode('_',$sValue);
				if(count($sValueTemp) >= 3 && count($userCookieTemp) >= 3){
					if($userCookieTemp[0] == $sValueTemp[0]
						&& $userCookieTemp[1] == $sValueTemp[1]
						&& $userCookieTemp[2] == md5($sValueTemp[1].$sValueTemp[2].'58shiyan')){
						return $userCookieTemp;
					}
				}
			}
		}
		if($jump){
			if($id == 'aid'){
				$url = U("admin/login/index");
			}else{
				$url = '/login';
			}
			$this->error("登录超时,请重新登录",$url);
		}else{
			return false;
		}
	}

	public $loginUserInfo = array();

	public function getUserInfo(){

		if(isset($this->loginUserInfo[0])){
			$uid = $this->loginUserInfo[0];
			$userModel = new AdminModel();
			$userData = $userModel->getUserById($uid);
			return $userData;
		}

		return array();
	}

	public function getInfoById($id){
		$infoModel = new InfoContentModel();
		$infoData = $infoModel->getInfoById($id);
		return $infoData;
	}
}
